<?php
/**
 * Created by PhpStorm.
 * User: Jacky
 * Date: 2015/11/16
 * Time: 14:06
 */

namespace Base\Model;

use Think\Model\RelationModel;
use Think\Page;

class BaseModel extends RelationModel
{

    /**
     * @param $id
     * 获取一个
     * @param bool $relation
     * @return mixed
     */
    public function getOne($id, $relation = false)
    {
        return $this->relation($relation)->where('status=1 and id=%d', $id)->find();
    }

    /**
     * @param string $where
     * @param bool $relation
     * @return mixed 获取所有
     * 获取所有
     */
    public function getAll($where = 'status=1', $relation = false)
    {
        return $this->relation($relation)->where($where)->order('id desc')->select();
    }

    /**
     * @param $size
     * @param bool $relation
     * @param string $where
     * @param null $parse
     * @param string $order
     * @return array 分页获取
     * 分页获取
     */
    public function pageToView($size, $relation = false, $where = 'status=1', $parse = null, $order = 'id asc')
    {
        $count = $this->where($where, $parse)->count();
        $Page = new Page($count, $size);
        $Page->lastSuffix = false;  //最后一页是否显示总页数
        $Page->rollPage = 5;    //分页栏每页显示的页数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '第一页');
        $Page->setConfig('last', '最后一页');
        $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $Page->show();
        $list = $this->where($where, $parse)->relation($relation)->order($order)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        return array(
            'list' => $list,
            'page' => $show,
        );
    }

    /**
     * @param mixed $page
     * @param mixed|null $size
     * @param bool|false $relation
     * @param string $where
     * @param string $order
     * @return mixed 分页获取
     * 分页获取
     */
    public function listByPage($page = 1, $size = 10, $relation = false, $where = 'status=1', $order = 'id desc')
    {
        $list = $this->relation($relation)->where($where)->page($page, $size)->order($order)->select();
        return $list;
    }
}